package madixin.diff;

import java.lang.reflect.Array;
import java.util.Arrays;

public class Test {

    public static int[] modify(int[] nums, int[][] modified) {
        for (int[] oneModify : modified) {
            for (int i = oneModify[0]; i <= oneModify[1]; i++) {
                nums[i] = nums[i] + oneModify[2];
            }
        }
        return nums;
    }

    public static int[] modifyDiff(int[] nums, int[][] modified) {
        int diff[] = new int[nums.length];
        // 构造差分数组
        diff[0] = nums[0];
        for (int i = 1; i < nums.length; i++) {
            diff[i] = nums[i] - nums[i - 1];
        }
        //System.out.println("----------");
        //System.out.println(Arrays.toString(diff));
        for (int[] oneModify : modified) {
            diff[oneModify[0]] += oneModify[2];

            if (oneModify[1] < nums.length - 1) {
                diff[oneModify[1] + 1] -= oneModify[2];
            }
        }
        //System.out.println(Arrays.toString(diff));
        //System.out.println("----------");
        // 还原差分数组
        nums[0] = diff[0];
        for (int i = 1; i < diff.length; i++) {
            nums[i] = nums[i - 1] + diff[i];
        }
        return nums;
    }


    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int[] numsDiff = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int[][] modified = {{2, 6, 1}, {3, 9, -3}, {0, 4, 2}};
        // int[][] modified = {{2, 6, 1}};
        System.out.println(Arrays.toString(nums));
        System.out.println(Arrays.toString(modify(nums, modified)));
        System.out.println(Arrays.toString(modifyDiff(numsDiff, modified)));
    }
}
